<?php

class Mediad2_Controller_ErrorAction extends Mediad2_Controller_Action
{
    private $_notifier;
    private $_error;
    private $_environment;
    private $_log;
    private $errorMessage;
    private $debug = true; //设置 
    
    public function init ()
    {
        parent::init();
        $bootstrap = $this->getInvokeArg('bootstrap');
        $environment = $bootstrap->getEnvironment();
        $error = $this->_getParam('error_handler');        
        $mailer = new Mediad2_Service_Util_Email();
        $session = new Zend_Session_Namespace();
        
        $this->_notifier = new Mediad2_Service_Util_NotifierError(
        		$environment, 
        		$error, 
        		$mailer, 
        		$session, 
        		$_SERVER
        );
        
        $this->_error = $error;
        $this->_environment = $environment;
        
        //定义result
        $this->result = new Mediad2_Model_Result();
        $this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE); //默认为失败
    }

    public function errorAction ()
    {
        $this->_log = $this->_getLog();
        
        switch ($this->_error->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $this->errorMessage = "对不起,你所访问的页面已不存在,或链接错误.请确认地址是否正确";
                // Log exception, if logger available
                if (isset($this->_log) && $this->_log != false) {
                    $this->_log->crit($this->_error->exception, 
                    $this->_error->exception);
                }
                $this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,$this->errorMessage);
                $this->encodeResult($this->result);
                break;
            
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->_applicationError();
                break;
        }
    
    }

    private function _applicationError ()
    {
        $fullMessage = $this->_notifier->getFullErrorMessage();
        $shortMessage = $this->_notifier->getShortErrorMessage();
        $this->errorMessage = nl2br($fullMessage);
        
        switch ($this->_environment) {
            case 'staging':
                $this->errorMessage = $fullMessage;
                break;
            case 'testing':
                $this->errorMessage = $fullMessage;
                break;
            default:
                $this->errorMessage = nl2br($fullMessage);
        }
        
        // Log exception, if logger available
        if (isset($this->_log) && $this->_log != false) {
            $this->_log->crit($this->errorMessage, $this->_error->exception);
        }
        
        // notify error
        $this->_notifier->notify();
        
        // goto view
        $request = $this->_error->request;
        $moduleName = $request->getModuleName();
        //TODO 其他模块的处理
        switch ($moduleName) {
            case "mobile":
                if ($this->debug) {
                    $this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,$this->errorMessage);
                } else {
                    $this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'系统故障，请联系客服');
                }
                $this->encodeResult($this->result);
                break;
            default:
                $this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'系统故障，请联系客服');
                $this->encodeResult($this->result);
                break;
        }
    }

    private function _getLog ()
    {
        $registry = Zend_Registry::getInstance();
        if (! $registry->isRegistered('log')) {
            return false;
        }
        $log = $registry->get('log');
        return $log;
    }
    

}
	